<link rel="stylesheet" href="<?=$this->baseUrl?>/css/partner.css">
<link rel="stylesheet" href="<?=$this->baseUrl?>/css/partner_edit.css">
<script type="text/javascript" src="<?=$this->base_url?>/js/cart.js"></script>
<script>

$(function () {
// --- Close Restoraunt Cart Empty ---------------------------------------------------------------//
	// autocount delivery price on load
	if (!$("#city_location").html().match("\,") && $.cookie('user_visicom_address')) {
		var _temp = $.parseJSON($.cookie('user_visicom_address'));
		$("#city_location").html(_temp.street + ', ' + _temp.house);
	}

	if ($("#city_location").html().match("\,")) {
		setTimeout(function() {
			$(".count_delivery_price").click();
		},500);
	}

	// count delivery price
	$("body").on("click",".count_delivery_price", function(e) {
		var div = $(this);
		var partner_id = $(".rest-block").attr("rest_id");

		$.get("/partner/ajax_get_delivery_price/?brand_id=" + partner_id, function(data) {
			var address = data;

			if (address != "noAdress") {
				if (parseFloat(data) !== 0 && !isNaN(parseFloat(data))) {
					div.siblings(".delivery_price").removeClass("hide").children("b").html(data);
				} else {
					div.siblings(".delivery_price").html('<b style="font-size: 21px;">'+data+'</b>').removeClass("hide");
				}
				div.fadeOut();
			} else if (address == "noAdress") {
				open_user_location_dialog(true);
			}

		});
	});
	// Help Pop-Up in Index and Sales
	var exit_access = false;
	var exit_href = '/';
	$('#bread-crumbs a, #logo').on('click', function() {
		if ($("#cart-empty").is(":visible")) {
			exit_access = true;
		}

		if (exit_access) {
			return true;
		} else {
			exit_href = $(this).attr('href');
			if ( $('#close_popup').length == 0) {
				$.get('/ajax_api/get_dialog/close_popup/', function(data){
					$('body').append(data);
					$('#close_popup').fadeIn('fast');
					$('.black-cover').fadeIn('fast');
				});
			} else {
				$('#close_popup').fadeIn('fast');
				$('.black-cover ').fadeIn('fast');
			}
			return false;
		}
	});

	$('body').on('click', '#close_popup .btn-foodda-green-small', function() {
		$('.black-cover').click();
	});

	$('body').on('click', '#close_popup .btn-foodda-gray-small', function() {
		$.get('/cart/ajax_cart_clear/', function() {
			location.href = exit_href;
		});

	});

	$('body').on('click', '#about_popup .btn-foodda-orange-big', function() {
		$('.black-cover').click();
	});


	// SEO toggle

	$('#rest-title').on('click', function(event) {
		$("#rest-header #seo_text").toggle();
		event.stopPropagation()
	});

	$('body').on('click', function() {
		$("#rest-header #seo_text").hide();
	});

	$("#rest-header #seo_text").on('click', function(event) {
		event.stopPropagation()
	});

// --- Scroll to item -------------------------------------------------------------------//
	var ids = document.location.href.replace(/.*#([\d]+)\/?i?d?([\d]+)?.*/i, '$1::::$2');
	if (ids.indexOf('::::') > 0) {
		tmp = ids.split("::::");
		var menu_id = tmp[0];
		var item_id = tmp[1];
		var get_sign = document.location.href.substr(-4,3);
		if (get_sign != 'get') {
			get_sign = document.location.href.substr(-3,3);
		}

		// Change menu and Scroll
		if (menu_id > 0) {
			$('#partner-menu-nav .float-holder a').removeClass('active');
			$('.menu_link[data-menu-id="' + menu_id + '"]').addClass('active');
			get_menu_data(menu_id, $('.menu_link[data-menu-id="' + menu_id + '"]').data('menu-view-type'));

			if (parseInt(item_id) > 0) {
				$(document).on('menuDataLoaded', function(e) {
					var one_callback = true;
					var selected_item = $(".item-holder[item_id=" + item_id + "], .dish_item .float-holder[item_id=" + item_id + "]");
					if (selected_item.length > 0) {
						$('html, body').animate({
							scrollTop : selected_item.offset().top
						}, 600, function() {
							if ((get_sign == 'get') && (one_callback)) {
								selected_item.children(".item-cart-btn").get(0).click();
								one_callback = false;
							}
						});
						$(document).off('menuDataLoaded');
					}
				});
			}
		}
	} else if (document.location.href.match(/.*#oh_commentable=(.+)/i) != null) {
		ids = document.location.href.replace(/.*#oh_commentable=(.+)/i, '$1');
		console.log(ids);
		if ($('#comment_register').length > 0) {
			$('#comment_register').remove();
		}
		$.get('/ajax_api/comment_register/?id=' + ids, function(data) {
			$('body').append(data);
			$('#comment_register').center().fadeIn('fast');
			$('.black-cover').fadeIn('fast');
			$('#comment_register').on('click', '.input-overed-right', function(e) {
				var _active = $('#comment_register .view-switcher:visible');
				var _inactive = $('#comment_register .view-switcher:hidden');

				_inactive.val(_active.val());

				$('#comment_register .view-switcher').toggle();
				$('#comment_register .ii-con').toggle();
				e.preventDefault();
				e.stopPropagation();
				return false;
			}).on('submit', 'form', function(e) {
					var _active = $('#comment_register .view-switcher:visible');
					var _inactive = $('#comment_register .view-switcher:hidden');
					_inactive.val(_active.val());
					$('#comment_register .error').html('').hide();
					$('html,body').css('opacity', .8);
					$('#global-loader').center(true, true, 99999).show();
					$(this).ajaxSubmit({ dataType : "JSON", success : function(data) {
						if (data.error) {
							$('html,body').css('opacity', 1);
							$('#global-loader').hide();
							$('#comment_register .error').html(data.error).slideDown(200);
							return false;
						}
						if (data.redirect) {
							document.location = data.redirect;
						} else {
							var url = document.location.href;
							if (document.location.href.indexOf('#') > 0) {
								url = document.location.href.slice(0, document.location.href.indexOf('#'));
							}
							document.location = url;
						}
					} });

					return false;
				});
			$('#comment_register input[name="phone"]').inputmask({
				mask: '(999) 999-99-99'
			});
			$('#comment_register input[type="password"]').focus();
		});
	}

// --- Menu Change --------------------------------------------------------------------------------//
	$('body,html').on('click', '#custom_popup #cutom-yes-button', function(e) {
		$('.black-cover').click();
	});

	$('#partner-menu-nav .float-holder a').on('click', function(e) {
		var _current_object = $(this);
		$('#partner-menu-nav .float-holder a').removeClass('active');
		$(this).addClass('active');
		get_menu_data(_current_object.attr('menu_id'), _current_object.data('menu-view-type'));
		cart_changed();
	});

// --- Item IMG -----------------------------------------------------------------------------------//
	$('body').on('click', '.item-img', function(e) {
		// alert ("Show me Big Image of Item ID: "+$(this).parent().attr('item_id'));
		var this_popover = $(this).parent().find('.popover').html();
		$('.popover').remove();
		if (typeof this_popover === 'undefined')
		{
			var item_name = $(this).parent().find('.item_name').html();
			var big_img = $(this).parent().attr('big_img');

			if (typeof big_img === 'undefined') big_img = '/img/no-img-big.png';

			var item_descr = $(this).parent().children('.decription').html();

			if (typeof item_descr === 'undefined') item_descr = '';
			else item_descr = '<br />'+item_descr;

			var zoom_image = '<div class="popover fade right in green-borders">'+
				'<div class="arrow"></div>'+
				'<img src="'+big_img+'" class="green-borders"/>'+
				'<div class="popover-content"><span class="green">'+item_name+'</span>'+item_descr+'</div>'+
				'<div class="btn-foodda-green-small fright" style="display: inline-block;">В КОРЗИНУ</div>'+
				'</div>';

			$(this).after(zoom_image);


			var tmp_obj = $(this).parent().children('.popover');

			if ($(this).parent().hasClass('float-holder'))
			{
				var new_top = (-1)*(tmp_obj.height()/2) + 15;
				var new_left = 78;
				tmp_obj.css( "top", new_top );
				tmp_obj.css( "left", new_left );
			}
			else
			{
				var new_top = (-1)*(tmp_obj.height()/2) + 65;
				var new_left = 200;
				tmp_obj.css( "top", new_top );
				tmp_obj.css( "left", new_left );
			}

			// Repeat for after load images
			setTimeout (function() {
				if (tmp_obj.parent().hasClass('float-holder'))
				{
					var new_top = (-1)*(tmp_obj.height()/2) + 15;
					var new_left = 78;
					tmp_obj.css( "top", new_top );
					tmp_obj.css( "left", new_left );
				}
				else
				{
					var new_top = (-1)*(tmp_obj.height()/2) + 65;
					var new_left = 200;
					tmp_obj.css( "top", new_top );
					tmp_obj.css( "left", new_left );
				}
			}, 300);
		}
		e.stopPropagation();
	});

	$('body').on('click', '.popover', function(e) {
		e.stopPropagation();
	});

	$('body').on('click', function(e) {
		$('.popover').remove();
	});


	$('body').on('click', '.item-holder' ,function(e) {
		// $(this).children('.item-cart-btn').click();
		$(this).children('.item-img').click();
		e.stopPropagation();
	});

	$('.item-info').tooltip();

	$('body').on('click', '.popover img' ,function(e) {
		$(this).parent().parent().children('.item-cart-btn').click();
		e.stopPropagation();
	});

	$('body').on('click', '.popover .btn-foodda-green-small' ,function(e) {
		$(this).parent().parent().children('.item-cart-btn').click();
		e.stopPropagation();
	});

// End Ready Functions
});

// AJAX Menu Load
function get_menu_data(menu_id, view_type) {
	if (!view_type) view_type = $('.rest-block').attr('list_view');
	var rest_id=$(".rest-block").attr("rest_id");
	$('#menu_items').fadeTo("fast", "0.15", function() {

		$("#partner-menu-nav .loading_img").css({ bottom: '-100px' });
		$('#partner-menu-nav').append('<img class="loading_img" src="/img/ajax-loader_bar2.gif" />');
		/* ------------ Send AJAX ------------- */

		var options = {
			url: "<?=CHtml::normalizeUrl(array('menu/view'))?>?cat_id=" + menu_id + '&rest_id=' + rest_id,
			success: function(data) {
				$('#menu_items').html(data);
				$('#menu_items').fadeTo("fast", "1", function() {
					$('#partner-menu-nav .loading_img').remove();
				});
				$('.item-info').tooltip();
				$.event.trigger({ type: 'menuDataLoaded', menu_id : menu_id, view_type : view_type });
			}
		};
		$("#partner-menu-nav").ajaxSubmit(options);
	});
}


</script>
<div id="bread-crumbs">
	<? $this->widget('zii.widgets.CBreadcrumbs', array(
		'links'=>$this->bread,
		'htmlOptions'=>array('class'=>'container'),
		'separator'=>'<i>→</i>'
	)); ?>
</div>
<div id="rest-container">
	<div class="container">
		<div class="float-holder">
			<div id="rest-holder">
				<div id="rest-header">
					<h2 id="rest-title">Доставка от <?=$rest->name?></h2>
					<div id="seo_text" class="green-borders">
						<?=$rest->body?>
					</div>


				</div>
				<div class="rest-block" list_view="0" rest_id="<?=$rest->id?>">

					<a class="rest-logo" href="<?=CHtml::normalizeUrl(array('menu/index', 'rest'=>$rest->id))?>" style="background: url('<?= $rest->img?>') no-repeat center center;"></a>

					<a class="rest-title" href="<?=CHtml::normalizeUrl(array('menu/index', 'rest'=>$rest->id))?>"><?=$rest->name?></a>
					<div class="rest-cats"><?=$rest->title?></div>

					<div class="rest-delivery-time" data-general-sum="60"><b><?=$rest->minimal_order?></b> тенге.</div>
					<div class="rest-delivery-time-info">Минимальный заказ</div>
					<div class="place-for-delivery-price">
						<div class="rest-delivery-time delivery-calc-position delivery_price"><?=($rest->delivery==0)?'<b class="font-size-20">бесплатно</b>':("<b>".$rest->delivery."</b> тенге.")?></div>
						<div class="rest-delivery-time-info delivery-calc-position">Стоимость доставки</div>
					</div>


					<div class="rest-delivery-time partner-mood-bar-position">

						<? if (count($rest->reviews)): ?>
							<div class="black"><?=count($rest->reviews)?> отзывов</div>
							<div class="mood-bar-parent">
								<div class="mood-bar like" style="width: 100%;"></div>
								<div class="mood-bar dislike" style="width: 0%;"></div>
							</div>
							<div class="mood-smiles-parent">
								<div class="positive">
									<img src="/img/smile_win_small.png" align="left">
									<?=count($rest->reviews(array('type'=>1)))?>
								</div>
								<div class="negative">
									<img src="/img/smile_fail_small.png" align="left">
									<?=count($rest->reviews(array('type'=>0)))?></div>
							</div>
						<? else: ?>
							<div class="lightgray no-comments">нет отзывов</div>
							<div class="mood-bar-parent">
								<div class="mood-bar like" style="width: 0%;"></div>
								<div class="mood-bar dislike" style="width: 0%;"></div>
							</div>
						<? endif ?>
					</div>

					<div class="rest-bottom-line"></div>
					<div class="rest-info">

						<? if ($rest->bonus): ?>
							<span>Бонус за заказ <?=$rest->bonus?>%</span>
						<? endif ?>

					</div>
					<?$time = explode('-', $rest->time_work);
					if(!($time[0]<  date('H:i')&& date('H:i')<$time[1])){
						$rest_closed=true;
					}
					?>
					<div class="rest-open-time <?=isset($rest_closed)?'closed':''?>">
						<i class="<?=isset($rest_closed)?'clock-icon-red':'clock-icon'?>"></i>
						<?=($time[0]==$time[1])?'Круглосуточно':$rest->time_work?>
					</div>
					<div class="rest-open"><?=isset($rest_closed)?'Закрыто':'Открыто'?></div>

					<div id="rest-tabs">
						<div id="tabs-line">
							<a class="tab" href="<?=CHtml::normalizeUrl(array('menu/index', 'rest'=>$rest->id))?>">
								<div class="tab-left"></div>
								<div class="tab-center">МЕНЮ</div>
								<div class="tab-right"></div>
							</a>

							<a class="tab-a" href="<?=CHtml::normalizeUrl(array('menu/reviews', 'rest'=>$rest->id))?>">
								<div class="tab-left"></div>
								<div class="tab-center">ОТЗЫВЫ</div>
								<div class="tab-right"></div>
							</a>
							<? if ($rest->actions(array('condition'=>'`date`>:time', 'params'=>array(':time'=>date('Y-m-d'))))): ?>
								<a class="tab" href="<?= CHtml::normalizeUrl(array('menu/actions', 'rest' => $rest->id)) ?>">
									<div class="tab-left"></div>
									<div class="tab-center">АКЦИИ</div>
									<div class="tab-right"></div>
								</a>
							<? endif ?>

						</div>
					</div>
					<script>
						$(function () {
							$(".rest-commets-right-mess .rest-commets-send").on("click", function() {

								var _obj = $(this);
								$('.rest-commets-right-mess .error').html('');
								var options = {
									url: "<?=CHtml::normalizeUrl(array('menu/AjaxReviews'))?>",
									beforeSend : function() {
										if ($('#rest-commets-textarea').val().length < 4) {
											$('.rest-commets-right-mess .error').hide().html('Оставьте Ваш отзыв.').slideDown(300);
											return false;
										}
										if ($('input[name="feedMark"]').val() != '1' && $('input[name="feedMark"]').val() != '0') {
											$('.rest-commets-right-mess .error').hide().html('Оставьте оценку.').slideDown(300);
											return false;
										}
										return true;
									},
									success: function(data) {
										if (data && data.indexOf('}') > 0) {
											data = $.parseJSON(data);
										}
										if (data.error) {
											_obj.parent().html(data.error);
										} else {
											_obj.parent().html("Спасибо, ваш комментарий отправлен.");
										}
										$('#rest-commets-textarea').val('').attr('disabled', true);
									},
									error : function() {
										_obj.parent().html("Ваш комментарий не отправлен, попробуйте еще раз позднее.");
									}
								};
								$("#send_comment").ajaxSubmit(options);
							});
						});
					</script>
					<style>
						.rest-commets-right-mess .error {
							color: red;
							left: 20px;
							bottom: -1px;
							display: none;
							position: absolute;
						}
					</style>
					<div id="rest-commets">
						<div class="rest-commets-title green">Оставить отзыв</div>
						<form id="send_comment" method="get" class="float-holder">
							<textarea id="rest-commets-textarea" <?=$reviews_open?'':'disabled="disabled"'?>  name="userFeed" placeholder="Отзыв не длиннее 1000 символов"></textarea>
							<input type="hidden" name="rest" value="<?=$rest->id?>">
							<input type="hidden" name="feedMark" id="feedMark">
							<div class="rest-commets-right-mess">
								<?if($reviews_open): ?>
								<div class="error"></div>
								<div class="btn-group" >
									<div class="btn" id="like" onclick="javascript: $('#dislike').removeClass('active'); $('#like').addClass('active'); $('#feedMark').val(1);">
										<a class="like_big"></a>
									</div>
									<div class="btn" id="dislike" onclick="javascript:$('#like').removeClass('active');  $('#dislike').addClass('active'); $('#feedMark').val(0);">
										<a class="dislike_big"></a>
									</div>
								</div>
									<a class="green-send-btn rest-commets-send" ></a>
								<?else:?>
									Отзывы можно <br>оставлять, только <br>после доставленного <br>и оплаченного заказа <br>этого партнера, один отзыв за один заказ.
								<?endif;?>
							</div>
						</form>
						<div class="rest-commets-title green">Отзывы о <?=$rest->name?></div>
						<div id="rest-commets-ajax-data">

							<? foreach($rest->reviews as $review): ?>
							    <div class="rest-commets-holder green-borders">
								<div class="<?=$review->type?'like_big':'dislike_big'?>" style="position: absolute;"></div>
								<div class="rest-commets-holder-title">
									<b><?=$review->idUser->name?></b> [<?= Yii::app()->dateFormatter->format('d MMMM yyyy HH:mm', $review->date) ?>]
								</div><?=$review->body?></div>
							<? endforeach ?>

						</div>
					</div>

				</div>
			</div>

			<div id="cart" data-order-type="0" style="margin-top: 20px;" class="">
				<div id="cart-top-bg">
					<div id="cart-title">КОРЗИНА</div>
					<div id="cart-icon"></div>
				</div>
				<div id="cart-empty">
					Пора<br>перекусить!!!
				</div>
				<div id="cart_items_holder">
					<? if (isset($carts)): ?>
						<? foreach ($carts as $cart): ?>
							<div class="cart-item" item_id="<?=$cart->id?>" style="">
								<div class="cart-item-title"><?=$cart->title?></div>
								<div class="cart-item-price"><b><?=$cart->price*$count_cart[$cart->id]?></b> тенге.</div>
								<div class="cart-item-cnt"><b><?=$count_cart[$cart->id]?></b> шт.</div>
								<a class="x-icon"></a>
								<a class="cart-item-minus"></a>
								<a class="cart-item-plus"></a>
							</div>
						<? endforeach ?>
					<? endif ?>
				</div>
				<div id="cart-summ-no-min" style="">
					<div id="cart-summ-no-min-info">
						<div class="att-icon"></div>
						<div class="red">Ресторан принимает<br> заказы от <span class="cart_min_summ"><?=$rest->minimal_order?></span> тенге</div>
						<div class="green">Добавьте еще несколько<br> блюд на <span>00</span> тенге</div>
					</div>
					<div id="cart-summ-no-min-text">
						Стоимость блюд: <b class="cart_summ">00</b> тенге.
						<div id="cart-no-ok"></div>
					</div>
				</div>

				<div id="cart-summ" style="display: none;">
					<div id="cart-summ-text">
						Стоимость блюд: <b class="cart_summ">163</b> тенге.
						<a id="cart-ok" class="btn-foodda-orange-big" href="/cart/">ЗАКАЗАТЬ</a>
					</div>
				</div>
			</div>			<div class="clean"></div>
		</div>
	</div>
</div>